<template>
    <div class="e-form">
        <BillTop title="运行记录新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData" label-width="180px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="记录（结算）日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                            <span> {{userInfo.orgInfo.orgName}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="设备名称：" prop="equipmentName">
                            <el-input v-model="formData.equipmentName"  readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectEquipmentName"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="计量单位：">
                            <span> {{formData.unit}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="设备编号：">
                            <span>{{formData.equipmentNo}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="规格型号：">
                            <span> {{formData.specificationModel}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="操作手：" prop="operation">
                            <el-input v-model="formData.operation"  readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectOperation"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="加油量（L）：">
                            <el-input v-model="formData.comeOn"
                            v-enter-float="{
                                set:this,
                                val:'formData',
                                key:'comeOn'
                            }"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                   <el-col :span="12">
                        <el-form-item label="记录人：">
                            <span> {{formData.recorder}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                          <span>{{ statusLable(formData.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                            <span> {{userInfo.orgInfo.orgName}}</span>
                        </el-form-item>
                    </el-col>
                </el-row> -->
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose" v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/dailyManagement.js'
import { mapState } from 'vuex'
import { selectHandleOperator, selectMoveEquipment } from '@/api/equip/popupCallback.js'
export default {
    data () {
        return {
            res: '',
            formData: {
                'billDate': '',
                'billId': '',
                'billNo': '',
                'comeOn': null,
                'equipmentId': '',
                'equipmentName': '',
                'equipmentNo': '',
                'founderId': '',
                'founderName': '',
                'isForeign': null,
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDescription': '',
                'nullifyReason': '',
                'operation': '',
                'operationId': '',
                'orgId': '',
                'orgName': '',
                'recorder': '',
                'specificationModel': '',
                'state': 0,
                'unit': '',
                'workId': '',
                'accountId': '',
                'source': 0, //判断来源是 0 本地 1 小程序
                'nature': '',
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择记录（结算）日期', trigger: 'change' },
                ],
                equipmentName: [
                    { required: true, message: '请选择设备名称', trigger: 'change'  },
                ],
                operation: [
                    { required: true, message: '请选择操作手', trigger: 'change'  },
                ],
            },
        }
    },
    components: {
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return obj ? obj.label : ''
            }
        },
    },
    created () {
        this.getInfo()
    },
    mounted () {

    },
    methods: {
        getInfo () {
            const date = new Date()
            this.formData.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            this.formData.recorder = localStorage.getItem('userName')
        },
        //选择设备名称
        selectEquipmentName () {
            let json = {
                'flag': true,
                'ids': [this.formData.accountId],
                'params': {
                    'orgId': this.formData.orgId,
                    'states': []
                }
            }
            selectMoveEquipment(json).then(res=>{
                this.res = res
                res.forEach(item=>{
                    this.formData.equipmentName = item.equipmentName
                    this.formData.unit = item.unit
                    this.formData.equipmentNo = item.equipmentNo
                    this.formData.specificationModel = item.specificationModel
                    this.formData.accountId = item.accountId
                    this.formData.nature = item.nature
                })
            })
        },
        //选择操作手
        selectOperation () {
            let ids = [this.formData.operationId]
            selectHandleOperator(ids).then(res=>{
                this.formData.operation = res[0].operator
                this.formData.operationId = res[0].billId
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if(valid) {
                    request.operationLogAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/operationLogEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                }else{
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/operationLogList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
